package com.jing.system.comm.contants;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

import org.apache.log4j.Logger;

import com.jing.system.utils.SpringUtil;
import com.jing.weibo.pojo.pb.PbType;
import com.jing.weibo.pojo.system.Grade;
import com.jing.weibo.pojo.system.Rule;
import com.jing.weibo.service.pb.PbTypeService;
import com.jing.weibo.service.system.GradeService;
import com.jing.weibo.service.system.RuleService;

/**
 * 数据库常用信息的类
 * @author jing.yue
 * @version 1.0
 * @since 2012-09-27
 */
public class DataCons {
	private static final Logger logger = Logger.getLogger(DataCons.class);

	/**
	 * 等级信息
	 * @see key: 	等级对象的编号<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	 * 			 		值为Grade对象的id属性<br>
	 * 		value: 	对应等级的等级对象
	 */
	public static Map<Integer, Grade> gradeMap;

	/**
	 * 初始化等级信息
	 * @param config 
	 */
	public static void initGradeMap(ServletConfig config) {
		GradeService gradeService = (GradeService) SpringUtil.getBean("gradeService");
		gradeMap = new HashMap<Integer, Grade>();
		try {
			//获取所有等级信息集合
			List<Grade> grades = gradeService.findAllGrade();
			for (Grade grade : grades) {
				gradeMap.put(grade.getId(), grade);
			}
			config.getServletContext().setAttribute("grades", grades);
			logger.info("初始化数据库等级信息成功: SUCCESS");
		} catch (Exception e) {
			logger.error("初始化等级信息异常!", e);
		}
	}

	/**
	 * 规则信息
	 * @see key: 	规则的Code<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	 * 			 		值为Rule对象的code属性<br>
	 * 		value: 	对应规则的对象
	 */
	public static Map<String, Rule> ruleMap;

	/**
	 * 初始化规则信息
	 * @param config
	 */
	public static void initRuleMap(ServletConfig config) {
		RuleService ruleService = (RuleService) SpringUtil.getBean("ruleService");
		ruleMap = new HashMap<String, Rule>();
		try {
			//获取所有等级信息集合
			List<Rule> rules = ruleService.findAllRule();
			for (Rule rule : rules) {
				ruleMap.put(rule.getCode(), rule);
			}
			config.getServletContext().setAttribute("rules", rules);
			logger.info("初始化数据库规则信息: SUCCESS");
		} catch (Exception e) {
			logger.error("初始化规则信息异常!", e);
		}
	}

	/**
	 * 泡吧分区
	 * @see key: 	分区的ID<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	 * 			 		值为PbType对象的id属性<br>
	 * 		value: 	泡吧分区对象
	 */
	public static Map<Integer, PbType> pbTypeMap;

	/**
	 * 初始化泡吧分区信息
	 * @param config
	 * @param servletContext 
	 */
	public static void initPbTypeMap(ServletConfig config, ServletContext servletContext) {
		PbTypeService pbTypeService = (PbTypeService) SpringUtil.getBean("pbTypeService");
		pbTypeMap = new HashMap<Integer, PbType>();
		try {
			//获取所有等级信息集合
			List<PbType> pbTypes = pbTypeService.findAllPbType();
			for (PbType pbType : pbTypes) {
				pbTypeMap.put(pbType.getId(), pbType);
			}
			if(config != null) {
				config.getServletContext().setAttribute("pbTypes", pbTypes);
			} else {
				servletContext.setAttribute("pbTypes", pbTypes);
			}
			logger.info("初始化数据库泡吧分区信息: SUCCESS");
		} catch (Exception e) {
			logger.error("初始化泡吧分区信息异常!", e);
		}
	}
}